import Notify from '@vant/weapp/notify/notify'
import Dialog from '@vant/weapp/dialog/dialog'


// 缓存信息
export function setStorage(key, value) {
    wx.setStorageSync(key, value)
}

// 获取缓存信息
export function getStorage(key) {
    try {
        var value = wx.getStorageSync(key)
        if (value) {
            return value
        } else {
            return null
        }
    } catch (e) {
        console.log(e);
    }
}

/**
 * Promise 形式的showToast
 * @param { object } param0 参数
 */
export const showToast = ({ title, icon = 'none' }) => {
    return new Promise((resolve, reject) => {
        wx.showToast({
            title,
            icon,
            duration: 1500,
            success: (res) => {
                resolve(res)
            },
            fail: () => { },
            complete: () => { }
        });
    })
}

/**
 * Promise 形式的showModel
 * @param { object } param0 参数
 */
export const showModel = ({ content, showCancel = true }) => {
    return new Promise((resolve, reject) => {
        wx.showModal({
            title: '提示',
            content,
            showCancel: showCancel,
            success: (res) => {
                resolve(res)
            },
            fail: (err) => {
                reject(err)
            }
        });
    })
}

/**
 * 显示顶部红色通知
 * 使用方法：调用时确保界面上有：
 * <van-notify id="van-notify"/>
 * @param msg
 * @param showTime
 */
export const showNotify = (ctx, message = '', type = 'primary', duration = '3000') => {
    Notify({
        message,
        type,
        duration,
        context: ctx,
        selector: "#van-notify"
    });
}


/**
 * 弹窗提示信息
 * 使用方法：调用时确保界面上有：
 * <van-dialog id="van-dialog"/>
 * @param {*} title
 * @param {*} message
 */
export const showDialog = (ctx, title = '提示', message = '无内容') => {
    Dialog.alert({
        title,
        message,
        context: ctx,
        selector: "#van-dialog"
    });
}

